{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button id="btnAddAuth" class="layui-btn icon-btn {:auth('mall.store_cate/add')}"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table id="tableAuth"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarAuth">
    <a class="layui-btn layui-btn-primary layui-btn-xs {:auth('mall.store_cate/edit')}" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs {:auth('mall.store_cate/delete')}" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelAuth">
    <form id="modelAuthForm" lay-filter="modelAuthForm" method="post" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <input name="type" type="hidden" value="4"/>
        <div class="layui-form-item">
            <label class="layui-form-label">上级分类</label>
            <div class="layui-input-block"  style="width: 190px">
                <select name="parent_id" lay-search >
                    <option value="0">一级分类</option>
                    {foreach $otherData as $vo}
                    <option value="{$vo.id}">{$vo.name}</option>
                    {foreach $vo.childs as $child}
                    <option value="{$child.id}">------|{$child.name}</option>
                    {/foreach}
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" value="" lay-verify="required" placeholder="请输入名称" class="layui-input" >
            </div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">排序</label>
            <div class="layui-input-inline">
                <input type="number" name="sort" value="" lay-verify="required|number" placeholder="请输入数字" class="layui-input" >
                <input type="hidden" name="id" value=""  class="layui-input" >
            </div>
        </div>

        <div class="form-group-bottom text-center">
            <button class="layui-btn" lay-filter="modelSubmitAuth" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>

            <!--<button type="reset" class="layui-btn layui-btn-primary">&emsp;重置&emsp;</button>-->

        </div>
    </form>
</script>
<!-- js部分 -->
{include file='public/footer' /}
<script>
    layui.use(['layer', 'form', 'admin', 'treeTable', 'util'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var treeTable = layui.treeTable;
        var util = layui.util;

        // 渲染表格
        var insTb = treeTable.render({
            elem: '#tableAuth',

            tree: {
                iconIndex: 1,  // 折叠图标显示在第几列
                idName: 'id',  // 自定义id字段的名称
                pidName: 'parent_id',  // 自定义标识是否还有子节点的字段名称
                isPidData: true  // 是否是pid形式数据
            },
            cellMinWidth: 100,

            cols: [
                {field: 'id', title: 'Id'},
                {field: 'name', title: '名称', width: 260},
                {field: 'sort', title: '排序号', align: 'center', width: 100},
                {
                    templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间', align: 'center'
                },
                {
                    templet: function (d) {
                        return util.toDateString(d.updateTime);
                    }, title: '更新时间', align: 'center'
                },
                {templet: '#tableBarAuth', title: '操作', align: 'center', width: 160}
            ],
            reqData: function (data, callback) {
                $.get('{:__url("mall.store_cate/getData")}', function (res) {
                    callback(res.data);
                });
            }
        });

        // 搜索
        $('#btnSearchAuth').click(function () {
            var keyword = $('#edtSearchAuth').val();
            if (keyword) {
                insTb.filterData(keyword);
            } else {
                insTb.clearFilter();
            }
        });

        // 添加按钮点击事件
        $('#btnAddAuth').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        treeTable.on('tool(tableAuth)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.name);i
            }
        });

        // 显示表单弹窗
        function showEditModel(mAuth) {
            layer.full(admin.open({

                type: 1,
                title: (mAuth ? '修改' : '添加') + '课程分类',
                content: $('#modelAuth').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = mAuth ? '{:__url("mall.store_cate/edit")}' : '{:__url("mall.store_cate/add")}';
                    if (mAuth && mAuth.isMenu == '1') {
                        $('#modelAuthForm input[name="isMenu"][value="1"]').prop("checked", true);
                    }
                    form.val('modelAuthForm', mAuth);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelSubmitAuth)', function (data) {
                        if (data.field.pid == '') {
                            data.field.pid = '0';
                        }
                        layer.load(2);
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 1) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.refresh();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            }));
        }

        // 删除
        function doDel(authorityId, authorityName) {
            layer.confirm('确定要删除分类“' + authorityName + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (index) {
                layer.close(index);
                layer.load(2);
                $.get("{:__url('mall.store_cate/delete')}", {
                    id: authorityId
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 1) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.refresh();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

    });
</script>
</body>
</html>